// audio.js
var http = require('../../utils/http.js')
let app = getApp()
Page({
  data: {
    current: 0,
    swiper6Current: 0,
    autoplay: true,
    showScreen: false,
    loadingState: 0,
    innerAudioContext: '',
    audioShow: false,
    swiperId: 1,
    swiperItem1: {
      id: 1,
      img1: '',
      img2: '',
      img3: '',
      img4: '',
      img5: '',
      img6: '',
    },
    swiperItem2: {
      id: 2,
      img1: '../../static/images/1.png',
      img2: '../../static/images/1.png',
      img3: '../../static/images/1.png',
    },
    swiperItem3: {
      id: 3,
      img1: '../../static/images/1.png',
    },
    swiperItem4: {
      id: 4,
      img1: '../../static/images/1.png',
    },
    swiperItem5: {
      id: 5,
      img1: '../../static/images/1.png',
      img2: '../../static/images/1.png',
      img3: '../../static/images/1.png',
      img4: '../../static/images/1.png',
      img5: '../../static/images/1.png',
      img6: '../../static/images/1.png',
    },
    swiperItem6: {
      id: 6,
      img1: ['../../static/images/1.png', '../../static/images/kuang.png', '../../static/images/1.png'],
    },
    weddingDate: '2025年05月10号',
    maleName: '方良权',
    femaleNname: '陈艳华',
    location: '赵厝社区居委会',
    theme: '婚礼邀请函',
    barragesArr: [],
  },
  onLoad(options) {
    this.setData({
      loadingState: 0,

    })
    if (options.type) {
      wx.setStorageSync('type', options.type)
      app.globalData.type = options.type
    }
    let type = wx.getStorageSync('type') || ''
    http.get("marryInfo", {
      type: type
    }, false).then(res => {
      this.setData({
        showScreen: true,
        swiperList: res.data.swiperList,
        weddingDate: res.data.weddingDate,
        maleName: res.data.maleName,
        femaleNname: res.data.femaleNname,
        address: res.data.address,
        theme: res.data.theme,
        location: res.data.location,
        loadingState: 1,
        swiperItem1: res.data.swiperItem1,
        swiperItem2: res.data.swiperItem2,
        swiperItem3: res.data.swiperItem3,
        swiperItem4: res.data.swiperItem4,
        swiperItem5: res.data.swiperItem5,
        swiperItem6: res.data.swiperItem6,
        innerAudioSrc: res.data.innerAudioSrc,
      })
      app.globalData.type = type
      app.globalData.imageUrlFinally = res.data.imageUrlFinally
      app.globalData.bridegroomTel = res.data.bridegroomTel
      app.globalData.brideTel = res.data.brideTel
      app.globalData.location = this.data.location
      app.globalData.maleName = res.data.maleName
      app.globalData.femaleNname = res.data.femaleNname
      app.globalData.latitude = res.data.latitude
      app.globalData.longitude = res.data.longitude
      app.globalData.blessing = res.data.blessing
      app.globalData.address = res.data.address

      this.initAudio()
    })
    this.barragesList()
  },
  onReady: function (e) {
    // this.addBarrage()
  },
  onUnload: function (e) {
    this.audioPause()
    this.setData({
      showScreen: false
    })
  },
  onHide: function (e) {
    this.audioPause()
    this.setData({
      showScreen: false,
      swiperCurrent: '0',
      swiper6Current: '0'
    })
  },
  onShow() {
    if (this.data.innerAudioContext) {
      this.audioPlay()
    }
    this.setData({
      showScreen: true,

    })
  },
  initAudio() {
    const innerAudioContext = wx.createInnerAudioContext({
      useWebAudioImplement: false // 是否使用 WebAudio 作为底层音频驱动，默认关闭。对于短音频、播
    })
    innerAudioContext.src = this.data.innerAudioSrc // 'https://qnvideo.hunliji.com/ff333b19b4a20a96c53630cf9d3fcae8.mp3'
    this.setData({
      innerAudioContext,
      audioShow: true
    })
    innerAudioContext.onEnded(res => {
      if (this.data.audioShow) {
        this.audioPlay()
      }
    })
    this.audioPlay()
  },
  audioPlay: function () {
    this.data.innerAudioContext.play() // 播放
    this.setData({
      audioShow: true
    })
  },
  audioPause: function () {
    this.data.innerAudioContext.pause()
    this.setData({
      audioShow: false
    })
  },
  addBarrage() {
    const barrageComp = this.selectComponent('.barrage')
    this.barrage = barrageComp.getBarrageInstance({
      font: 'bold 16px sans-serif',
      duration: 10,
      lineHeight: 2,
      mode: 'separate',
      padding: [10, 0, 10, 0],
      tunnelShow: false
    })
    this.barrage.open()
    http.get("barragesList", {
      currentPage: -1
    }, false).then(res => {
      if (res.code == 200) {
        let list = res.data.list.map(item => ({
          content: item.title,
          color: 'rgb(185, 150, 130)'
        }))
        setTimeout(() => {
          this.barrage.addData(list)
        }, 1000)
      }
    }).catch(res => {})
  },
  barragesList() {
    http.get("barragesList", {
      currentPage: -1
    }, false).then(res => {
      if (res.code == 200) {
        console.log(res.data, 'res.data');
        let list = res.data.list.map((item, index) => {
          item.random = 120 + Math.floor(Math.random() * 200) + 'rpx';
          item.delay = index + index + 's'
          return item
        })
        this.setData({
          barragesArr: list
        })
      }
    }).catch(res => {})
  },
  swiperChange(val) {
    this.setData({
      swiperId: val.detail.currentItemId,
      swiperCurrent: val.detail.current,
      autoplay: val.detail.current < 5,
      swiper6Current: 0
    })
    console.log(val.detail.current, 'val.detail.current');
  },

  swiper6Change(val) {
    // console.log(val,'val');
    this.setData({
      swiper6Current: val.detail.current,
    })
    if (val.detail.current == this.data.swiperItem6.img1.length-1) {
      this.setData({
        autoplay: true,
      })
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    wx.showShareMenu({
      withShareTicket: true,
      menus: ['shareAppMessage', 'shareTimeline']
    });
    return {
      title: '诚意邀请你参加我们的婚礼',
      imageUrl: app.globalData.imageUrlFinally,
      path: "pages/pics/pics?type=" + app.globalData.type,
      success: function (res) {
        wx.showToast({
          title: '分享成功',
        })
      },
      fail: function (res) {
        // 转发失败
        wx.showToast({
          title: '分享取消',
        })
      }
    }
  },
  //用户点击右上角分享朋友圈
  onShareTimeline: function () {
    return {
      title: '诚意邀请你参加我们的婚礼',
      imageUrl: app.globalData.imageUrlFinally,
      query: 'type=' + app.globalData.type
    }
  },
})